package com.lzhsite.technology.designPattern.command.CalculatorDemo;

import java.util.ArrayList;
import java.util.List;

public class Calculator {
	private List<ICommand> undoCommands = new ArrayList<ICommand>();

	private List<ICommand> redoCommands = new ArrayList<ICommand>();

	private ICommand addCommand = null;
	private ICommand subtractCommand = null;

	public void setAddCommand(ICommand addCommand) {
		this.addCommand = addCommand;
	}

	public void setSubtractCommand(ICommand subtractCommand) {
		this.subtractCommand = subtractCommand;
	}

	public void addPressed() {
		this.addCommand.execute();
		undoCommands.add(this.addCommand);
	}

	public void subtractPressed() {
		this.subtractCommand.execute();
		undoCommands.add(this.subtractCommand);
	}

	public void undoPressed() {
		if (this.undoCommands.size() > 0) {
			ICommand command = this.undoCommands.get(undoCommands.size() - 1);
			command.undo();
			this.redoCommands.add(command);
			this.undoCommands.remove(command);
		} else {
			System.out.println("很抱歉，没有可撤销的命令");
		}
	}

	public void redoPressed() {
		if (this.redoCommands.size() > 0) {
			ICommand command = this.redoCommands.get(redoCommands.size() - 1);
			command.execute();
			this.undoCommands.add(command);
			this.redoCommands.remove(command);
		} else {
			System.out.println("很抱歉，没有可恢复的命令");
		}
	}
}
